<template>
  <div class="cash-box">
    <div class="font-size-16 font-weight-700 margin-bottom-10">{{ title }}</div>
    <div class="flex align-center">
      <div class="font-weight-700">本年计划</div>
      <div class="txt-box color-666666 font-weight-700">{{ model.currPlanAmount | moneyFilterNoUnit }}</div>
      <div class="bar-box relative-box plan-this-year" :style="[{ width: widthThisYearPlan + 'px' }]"></div>
    </div>

    <el-tooltip class="item" effect="dark" placement="top-end">
      <div class="flex align-center margin-top-bottom-15">
        <div class="font-weight-700">去年执行</div>
        <div class="txt-box color-666666 font-weight-700">{{ model.lastExecutionAmount | moneyFilterNoUnit }}</div>
        <div class="bar-box relative-box execute-last-year" :style="[{ width: widthExecuteLY + 'px' }]">
          <div class="bar-box absolute-box execute-last-year-diff" :style="[{ width: widthExecuteDiffLY + 'px' }]"></div>
          <div v-if="diffNumlastExecution != 0" class="diff-txt-box absolute-box" :style="[{left: widthExecuteDiffLY + 'px'}]">{{diffNumlastExecution | moneyFilterNoUnit}}</div>
        </div>
      </div>
      <div slot="content">
        <div>
          <div class="margin-bottom-15">
            <span>本年现金{{ isIncome ? '流入' : '支出' }}预测：</span>
            <span>{{ model.currPlanAmount | moneyFilterNoUnit }}</span>
          </div>
          <div class="margin-bottom-15">
            <span>去年执行：</span>
            <span>{{ model.lastExecutionAmount | moneyFilterNoUnit }}</span>
          </div>
          <div>
            <span>与本年计划差异：</span>
            <span>{{ model.lastExecutionDifferentAmount | moneyFilterNoUnit }}</span>
            <span class="padding-left-right-5">/</span>
            <span v-if="model.currPlanAmount != 0 && model.lastExecutionAmount == 0">本年新增</span>
            <span v-else-if="model.currPlanAmount == 0 && model.lastExecutionAmount != 0">本年删减</span>
            <span v-else-if="model.currPlanAmount == 0 && model.lastExecutionAmount == 0">--</span>
            <span v-else>{{ model.lastExecutionDifferent > 0 || model.lastExecutionDifferent == 0 ? '' : '-' }}{{ Math.abs(model.lastExecutionDifferent || 0) | accMul }}%</span>
          </div>
        </div>
      </div>
    </el-tooltip>

    <el-tooltip class="item" effect="dark" placement="top-end">
      <div class="flex align-center">
        <div class="font-weight-700">去年计划</div>
        <div class="txt-box color-666666 font-weight-700">{{ model.lastPlanAmount | moneyFilterNoUnit }}</div>
        <div class="bar-box relative-box plan-last-year" :style="[{ width: widtPlanLY + 'px' }]">
          <div class="bar-box absolute-box plan-last-year-diff" :style="[{ width: widtPlanDiffLY + 'px' }]"></div>
          <div v-if="diffNumlastPlan != 0" class="diff-txt-box absolute-box" :style="[{left: widtPlanDiffLY + 'px'}]">{{diffNumlastPlan | moneyFilterNoUnit}}</div>
        </div>
      </div>
      <div slot="content">
        <div>
          <div class="margin-bottom-15">
            <span>本年现金{{ isIncome ? '流入' : '支出' }}预测：</span>
            <span>{{ model.currPlanAmount | moneyFilterNoUnit }}</span>
          </div>
          <div class="margin-bottom-15">
            <span>去年计划：</span>
            <span>{{ model.lastPlanAmount | moneyFilterNoUnit }}</span>
          </div>
          <div>
            <span>与本年计划差异：</span>
            <span>{{ model.lastPlanDifferentAmount | moneyFilterNoUnit }}</span>
            <span class="padding-left-right-5">/</span>
            <span v-if="model.currPlanAmount != 0 && model.lastPlanAmount == 0">本年新增</span>
            <span v-else-if="model.currPlanAmount == 0 && model.lastPlanAmount != 0">本年删减</span>
            <span v-else-if="model.currPlanAmount == 0 && model.lastPlanAmount == 0">--</span>
            <span v-else>{{ model.lastPlanDifferent > 0 || model.lastPlanDifferent == 0 ? '' : '-' }}{{ Math.abs(model.lastPlanDifferent || 0) | accMul }}%</span>
          </div>
        </div>
      </div>
    </el-tooltip>
  </div>
</template>
<script>
  import { fmoneyThousand } from '@/common/js/utils';
  import Bar from '@c/echarts/bar';
  import { toFixedTwo } from '@/common/js/utils';

  export default {
    props: {
      title: {
        type: String,
        default: '',
      },
      model: {
        type: Object,
        default: () => {},
      },
      //是收入还是支出
      isIncome: {
        type: Boolean,
        default: true,
      },
    },

    watch: {
      model: {
        deep: true,
        handler(val) {
          this.initBar();
        },
      },
    },

    mounted() {
    },

    data() {
      return {
        widthThisYearPlan: 0, //本年计划的宽度
        widthExecuteLY: 0, //去年执行的宽度
        widthExecuteDiffLY: 0, //去年执行和本年计划的差值的宽度
        widtPlanLY: 0, //去年计划的宽度
        widtPlanDiffLY: 0, //去年计划和本年计划的差值的宽度
        diffNumlastExecution: 0,
        diffNumlastPlan: 0,
      };
    },

    methods: {
      //currPlanAmount 本年计划  lastExecutionAmount 去年执行  lastPlanAmount 去年计划
      initBar() {
        let baseWidth = document.getElementsByClassName('cash-box')[0].clientWidth; //宽度的基数
        baseWidth = baseWidth - 200; //减去padding 和 前面文字的宽度
        let baseNum = this.model.currPlanAmount > this.model.lastExecutionAmount ? this.model.currPlanAmount : this.model.lastExecutionAmount; //比较本年计划和去年执行的值大小，取大值
        baseNum = this.model.lastPlanAmount > baseNum ? this.model.lastPlanAmount : baseNum; //比较本年计划和去年计划的值大小，取大值为基数
        this.widthThisYearPlan = (this.model.currPlanAmount / baseNum) * baseWidth; //本年计划的宽度
        this.computeWidth('lastExecutionAmount', 'widthExecuteLY', 'widthExecuteDiffLY', baseNum, baseWidth);
        this.computeWidth('lastPlanAmount', 'widtPlanLY', 'widtPlanDiffLY', baseNum, baseWidth);

        this.diffNumlastExecution = toFixedTwo(this.model.lastExecutionAmount - this.model.currPlanAmount)
        this.diffNumlastPlan = toFixedTwo(this.model.lastPlanAmount - this.model.currPlanAmount)
      },

      computeWidth(compireFiledName, width, widthDiff, baseNum, baseWidth) {
        if (this.model[compireFiledName] >= this.model.currPlanAmount) {
          this[width] = (this.model[compireFiledName] / baseNum) * baseWidth; //计划 ｜ 执行值
          this[widthDiff] = (this.model.currPlanAmount / baseNum) * baseWidth; //计划 ｜ 执行差值
        } else {
          this[width] = this.widthThisYearPlan; //计划 ｜ 执行值
          this[widthDiff] = (this.model[compireFiledName] / baseNum) * baseWidth; //计划 ｜ 执行差值
        }
      },
    },
  };
</script>

<style lang="scss" scoped>
  .execute-last-year,
  .plan-last-year {
    background: rgb(235, 232, 232);
  }

  .plan-this-year {
    background: #4e9475;
  }

  .execute-last-year-diff {
    background: #b7e787;
  }

  .plan-last-year-diff {
    background: #82bd64;
  }

  .bar-box {
    height: 12px;
  }

  .diff-txt-box{
    top: -20px;
    color: #333333;
    font-size: 12px;
    white-space: nowrap;
  }

  .txt-box {
    width: 115px;
    padding-left: 30px;
  }
</style>
